# coding=utf-8
# -*- coding: utf-8 -*-

import threading
from urllib import request
import urllib.parse
import json
import string
import ssl
from douban.proxy import proxy

ssl._create_default_https_context = ssl._create_unverified_context

baseUrl = 'https://movie.douban.com/j/search_subjects?'


class tags_type(threading.Thread):  # The timer class is derived from the class threading.Thread
    def __init__(self):
        threading.Thread.__init__(self)
        self.url = 'https://movie.douban.com/j/search_tags?type=movie&source='
        self.url = urllib.parse.quote(self.url, safe=string.printable)

    def run(self):  # Overwrite run() method, put what you want the thread do here
        print(self.url)
        hdr = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
            'Accept-Encoding': 'none',
            'Accept-Language': 'en-US,en;q=0.8',
            'Connection': 'keep-alive'}
        opener = proxy()
        request.install_opener(opener.get_opener())
        req = request.Request(self.url, data=None, headers=hdr)
        f = request.urlopen(req)
        content = f.read().decode('utf-8')
        self.data = json.loads(content)
        print(self.data['tags'])

    def get_data(self):
        try:
            if len(self.data['tags']):
                return self.data['tags']
            return None
        except Exception:
            return None
